package com.neusoft.controller;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import com.neusoft.po.CommonResult;
import com.neusoft.po.Orders;
import com.neusoft.service.OrdersService;

//@CrossOrigin("*")
@RestController
@RequestMapping("/OrdersController")
@RefreshScope
public class OrdersController {

	@Autowired
	private OrdersService ordersService;

	@RequestMapping("/createOrders/{userId}/{businessId}/{daId}/{orderTotal}")
	public CommonResult<Integer> createOrders(@PathVariable("userId") String userId,
			@PathVariable("businessId") Integer businessId, @PathVariable("daId") Integer daId,
			@PathVariable("orderTotal") Double orderTotal) throws Exception {
		Orders orders = new Orders();
		orders.setUserId(userId);
		orders.setBusinessId(businessId);
		orders.setDaId(daId);
		orders.setOrderTotal(orderTotal);
		int orderId = ordersService.createOrders(orders);
		return new CommonResult(200, "success(10600)", orderId);
	}

	@GetMapping("/getOrdersById/{orderId}")
	public CommonResult<Orders> getOrdersById(@PathVariable("orderId") Integer orderId) throws Exception {
		Orders orders = ordersService.getOrdersById(orderId);
		return new CommonResult(200, "success(10600)", orders);
	}

	// order by orderId -> order by orderDate
	@GetMapping("/listOrdersByUserId/{userId}")
	public CommonResult<List> listOrdersByUserId(@PathVariable("userId") String userId) throws Exception {
		List<Orders> list = ordersService.listOrdersByUserId(userId);
		return new CommonResult(200, "success(10600)", list);
	}


	//返回值：受影响的行数 //按照orderId删除，成功为1，失败为0

	@DeleteMapping("/removeOrders/{orderId}")
	public CommonResult<Integer> removeOrders(@PathVariable("orderId") Integer orderId) throws Exception{
		Orders orders = new Orders();
		orders.setOrderId(orderId);
		int result = ordersService.removeOrders(orders);
		return new CommonResult<>(200, "success", result);
	}
}
